from py2neo import *


class AnswerSearcher(object):

    def __init__(self, url='bolt://119.91.156.232:7687', pwd='test'):
        self.graph = Graph(url, password=pwd)
        self.searcher_mapper = {
            '简介': self.searcher1,
            '别名': self.searcher2,
            '分布': self.searcher3,
            '颜色分类': self.searcher4,
            '开花季节': self.searcher5,
            '光照需求': self.searcher6,
            '培育难度': self.searcher7,
            '适宜温度': self.searcher8,
            '酸碱范围': self.searcher9,
            '作物特征': self.searcher10,
            '种植方式': self.searcher11,
            '繁殖方式': self.searcher12,
            '开花形态': self.searcher13,
            '防治措施': self.searcher14,
            '为害规律': self.searcher15,
            '症状': self.searcher16,
            '病因': self.searcher17,
            '农药毒性': self.searcher18,
            '农药用途': self.searcher19,
            # '主要防治对象和施用方法': self.searcher20,
            '农药使用方法': self.searcher20,
            '用量': self.searcher21,
            '使用方法/注意事项': self.searcher22,
            '属目': self.searcher23,
            '属科': self.searcher24,
            '所属类别': self.searcher25,
            # '防治农药': self.searcher26,
            '农药防止': self.searcher26,
            '传播途径和发病条件': self.searcher27,
            '为害对象': self.searcher28,
            '为害部位': self.searcher29,
            '为害症状': self.searcher30,
            '生活习性': self.searcher31,
            '形态特征': self.searcher32,
            '农药混配': self.searcher33,
            '区别': self.searcher34,
        }

    def get_graph(self):
        return self.graph

    def get_answer(self, query):
        intent = query['intent']['label']
        searcher = self.searcher_mapper.get(intent, self.searcher0)
        result = searcher(query)
        return result

    def delete_useless(self, ans):
        """删除Cypher查询结果中的无用信息"""
        ans = str(ans)
        space_location = self.find_second_space(ans, '\n')
        return ans[space_location + 1:]

    def find_second_space(self, ans, space):
        second_position = -1
        i = 0
        while i < 2:
            position = ans.find(space, second_position + 1)
            if position == -1:
                return second_position
            second_position = position
            i = i + 1
        return second_position

    def searcher0(self, query):
        """处理未匹配到搜索器的结果"""
        # return str(f'未匹配到搜索器!\n{query}')
        res = self.searcher1(query)
        if res == '':
            res = '也许你可以试点别的，比如，西瓜的光照条件？'
        return res

    def searcher1(self, query):
        """简介"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        drug = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.Pathogeny LIMIT 1"
                )
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.Pathogeny LIMIT 1"
                )
            else:
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})  RETURN n.introduce LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.Pathogeny LIMIT 1"
                )
                # ans = "/n是否不是您需要的答案，试试添加作物的名称再查询"
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.Pathogeny LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        elif drug != '':
            ans = self.graph.run(
                "MATCH (n) WHERE n.name =~ '.*" + drug + ".*' RETURN n.introduce LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher2(self, query):
        """别名"""
        """药物暂时没有别名"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        drug = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.Alias LIMIT 1"
                )
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.Alias LIMIT 1"
                )
            else:
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})  RETURN n.alias LIMIT 1"
                )
                # ans = self.graph.run(
                #     "MATCH (n) WHERE n.name =~ '.*" + crop + ".*' RETURN n.introduce LIMIT 1"
                # )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.Alias LIMIT 1"
                )
                # ans = "/n是否不是您需要的答案，试试添加作物的名称再查询"
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.Alias LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        elif drug != '':
            ans = self.graph.run(
                "MATCH (n) WHERE n.name =~ '.*" + drug + ".*' RETURN n.Alias LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher3(self, query):
        """分布"""
        """作物的分布在introduce，虫害的分部在DiseaseRule，病害没有分布，"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if dis != '':
            # ans = self.graph.run(
            #     "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.introduce LIMIT 1"
            # )
            ans = "该病害暂时没有收录分布地区"
        elif crop != '':
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN n.DiseaseRule LIMIT 1"
                )
            else:
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})  RETURN n.introduce LIMIT 1"
                )
                # ans = self.graph.run(
                #     "MATCH (n) WHERE n.name =~ '.*" + crop + ".*' RETURN n.introduce LIMIT 1"
                # )
        elif crop == '':
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.DiseaseRule LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher4(self, query):
        """颜色分类"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.color LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher5(self, query):
        """开花季节"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.season LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher6(self, query):
        """光照需求"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.light LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher7(self, query):
        """培育难度"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.difficulty LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher8(self, query):
        """适宜温度"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.temperature LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher9(self, query):
        """酸碱范围"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.acid LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher10(self, query):
        """作物特征"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.feature LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher11(self, query):
        """种植方法"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.plantMethod LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher12(self, query):
        """繁殖方式"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.bleed LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher13(self, query):
        """开花形态"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.bloom LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher14(self, query):
        """防治措施"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.PreventMethod LIMIT 1"
                )
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.PreventMethod LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.PreventMethod LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.PreventMethod LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher15(self, query):
        """为害规律"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.DiseaseRule LIMIT 1"
                )
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.DiseaseRule LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.DiseaseRule LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.DiseaseRule LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher16(self, query):
        """症状"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.Symptom LIMIT 1"
                )
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.Symptom LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.Symptom LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.Symptom LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher17(self, query):
        """病因"""
        """数据库目前没有病因"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        # if entities.get('Crop') is not None:
        #     crop = entities.get('Crop')[0]
        # if entities.get('Dis') is not None:
        #     dis = entities.get('Dis')[0]
        # if crop != '' and dis != '':
        #     ans = self.graph.run(
        #         "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.PreventMethod"
        #     )
        # else:
        #     return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher18(self, query):
        """农药毒性"""
        entities = query['entities']
        drug = ''
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if drug != '':
            ans = self.graph.run(
                "MATCH (n) where n.name =~ '.*" + drug + ".*' RETURN n.poison LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher19(self, query):
        """农药用途"""
        entities = query['entities']
        drug = ''
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if drug != '':
            ans = self.graph.run(
                "MATCH (n) where n.name =~ '.*" + drug + ".*' RETURN n.application LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        if "null" in str(ans):
            print('yeah')
        return self.delete_useless(ans)

    def searcher20(self, query):
        """主要防治对象和施用方法"""
        entities = query['entities']
        drug = ''
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if drug != '':
            ans = self.graph.run(
                "MATCH (n) where n.name =~ '.*" + drug + ".*' RETURN n.application LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        if "null" in str(ans):
            print('yeah')
        return self.delete_useless(ans)

    def searcher21(self, query):
        """用量"""
        entities = query['entities']
        drug = ''
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if drug != '':
            ans = self.graph.run(
                "MATCH (n) where n.name =~ '.*" + drug + ".*' RETURN n.application LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        if "null" in str(ans):
            print('yeah')
        return self.delete_useless(ans)

    def searcher22(self, query):
        """使用方法/注意事项"""
        entities = query['entities']
        drug = ''
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if drug != '':
            ans = self.graph.run(
                "MATCH (n) where n.name =~ '.*" + drug + ".*' RETURN n.application LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        if "null" in str(ans):
            print('yeah')
        return self.delete_useless(ans)

    def searcher23(self, query):
        """属目"""
        ans = '暂无此搜索器'
        return self.delete_useless(ans)

    def searcher24(self, query):
        """属科"""
        ans = '暂无此搜索器'
        return self.delete_useless(ans)

    def searcher25(self, query):
        """所属类别"""
        entities = query['entities']
        crop = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if crop != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})  RETURN n.cropKind LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher26(self, query):
        """防治农药"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH p=(n)-[r:`防治`]->(m) WHERE m.name =~ '.*" + dis + ".*' RETURN n.name LIMIT 5"
                )
            elif pest != '':
                ans = self.graph.run(
                    "MATCH p=(n)-[r:`防治`]->(m) WHERE m.name =~ '.*" + pest + ".*' RETURN n.name LIMIT 5"
                )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher27(self, query):
        """传播途径与发病条件"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.DiseaseRule LIMIT 1"
                )
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.DiseaseRule LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.DiseaseRule LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.DiseaseRule LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher28(self, query):
        """为害对象"""
        entities = query['entities']
        dis = ''
        pest = ''
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if dis != '':
            ans = self.graph.run(
                "MATCH p=(n)-[r:`为害`]->(m) WHERE n.name =~ '.*" + dis + ".*' RETURN m.name LIMIT 5"
            )
        elif pest != '':
            ans = self.graph.run(
                "MATCH p=(n)-[r:`为害`]->(m) WHERE n.name =~ '.*" + pest + ".*' RETURN m.name LIMIT 5"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher29(self, query):
        """为害部位"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.harm_part LIMIT 1"
                )
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.harm_part LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.harm_part LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.harm_part LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher30(self, query):
        """为害症状"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.Symptom LIMIT 1"
                )
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.Symptom LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.Symptom LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.Symptom LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher31(self, query):
        """生活习性"""
        entities = query['entities']
        crop = ''
        pest = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if crop != '' and pest != '':
            ans = self.graph.run(
                "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.Symptom LIMIT 1"
            )
        elif crop == '' and pest != '':
            ans = self.graph.run(
                "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.Symptom LIMIT 1"
            )
            ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher32(self, query):
        """形态特征"""
        entities = query['entities']
        crop = ''
        dis = ''
        pest = ''
        drug = ''
        ans = ''
        if entities.get('Crop') is not None:
            crop = entities.get('Crop')[0]
        if entities.get('Dis') is not None:
            dis = entities.get('Dis')[0]
        if entities.get('Pest') is not None:
            pest = entities.get('Pest')[0]
        if entities.get('Drug') is not None:
            drug = entities.get('Drug')[0]
        if crop != '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + dis + ".*' RETURN m.Pathogeny LIMIT 1"
                )
            elif pest != '':
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})--(m) where m.name =~ '.*" + pest + ".*' RETURN m.Pathogeny LIMIT 1"
                )
            else:
                ans = self.graph.run(
                    "MATCH (n{name:'" + crop + "'})  RETURN n.introduce LIMIT 1"
                )
        elif crop == '':
            if dis != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + dis + ".*' RETURN n.Pathogeny LIMIT 1"
                )
                # ans = "/n是否不是您需要的答案，试试添加作物的名称再查询"
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
            if pest != '':
                ans = self.graph.run(
                    "MATCH (n) WHERE n.name =~ '.*" + pest + ".*' RETURN n.Pathogeny LIMIT 1"
                )
                ans = str(ans) + "\n不是您需要的答案？试试添加作物的名称再进行查询，获取您需要的答案（^_^）"
        elif drug != '':
            ans = self.graph.run(
                "MATCH (n) WHERE n.name =~ '.*" + drug + ".*' RETURN n.introduce LIMIT 1"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher33(self, query):
        """农药混配"""
        entities = query['entities']
        i = 0
        drug = []

        while i < len(entities.get('Drug')):
            drug.append(entities.get('Drug')[i])
            i = i + 1
        if drug[0] != '' and drug[1] != '':
            ans = self.graph.run(
                "MATCH p=(n)-[r:`混配`]->(m) WHERE n.name =~ '.*" + drug[0] + ".*' AND m.name =~ '.*" + drug[1]
                + ".*' RETURN p"
            )
        else:
            return str(f'缺少必要参数!\n{query}')
        return self.delete_useless(ans)

    def searcher34(self, query):
        """区别"""
        ans = '暂无此搜索器'
        return self.delete_useless(ans)


if __name__ == '__main__':
    demo = {
        'text': '西瓜病毒病可以用多菌灵吗',
        'intent': {
            'label': '简介',
            'confidence': 1.0000098943710327
        },
        'entities': {
            'Crop': ['番茄'],
            'Dis': ['病毒病'],
            'Drug': ['2，4-滴丁酯', '莠去津']
        }
    }
    answer_searcher = AnswerSearcher()
    # print(answer_searcher.get_answer(demo))
    res = answer_searcher.get_graph().run(
        '''match path=(m)-[r]->(p) where m.name = '西瓜' or p.name = '西瓜' return relationships(path) limit 1''')
    res = list(res)
    print(res)
